package com.compmov.socialnetwork;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import com.compmov.datamodel.Persona;
import com.compmov.libraries.LazyAdapter;
import com.compmov.libraries.PersonaAdapter;
import com.compmov.webservice.PersonaWSClient;
import com.compmov.webservice.WSClientException;

public class AmigosBuscados extends Activity {

	private ListView list;
	LazyAdapter adapter=null;
	PersonaAdapter adapterP=null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.amigos);

		Bundle bundle = getIntent().getExtras();
		Persona per=new Persona();
		boolean completo=false;
		
		if (bundle != null) {
			per.setId(bundle.getInt("idPer"));
			per.setNombreUsuario(bundle.getString("usuario"));
			per.setNombrePersona(bundle.getString("nombre"));
			per.setApellidoPaterno(bundle.getString("apaterno"));
			per.setApellidoMaterno(bundle.getString("amaterno"));
			per.setCorreo(bundle.getString("correo"));
			
			if(bundle.getString("usuario").length()==0&&
					bundle.getString("nombre").length()==0&&
					bundle.getString("apaterno").length()==0&&
					bundle.getString("amaterno").length()==0&&
					bundle.getString("correo").length()==0){
				completo=true;
			}
			
		}
		
		list = (ListView) findViewById(R.id.list);
		PersonaWSClient personaWS = new PersonaWSClient("persona");
		
		try{
			List<Persona> personas;
			
			if(per.getId()==0){
				if(completo){
					personas=personaWS.getAll();
					
				}else{
					personas=personaWS.buscaPersonas(per);
				}
				adapterP = new PersonaAdapter(this, personas);
				list.setAdapter(adapterP);				
				
			}else{
				if(completo){
					personas=personaWS.getAllFriends(per);
					
				}else{
					personas=personaWS.buscaPersonas(per);
				}
				adapter = new LazyAdapter(this, personas);
				list.setAdapter(adapter);				
			}
					
		} catch (WSClientException e) {
			Toast.makeText(getBaseContext(),"Error en el servidor. Intente de nuevo",
					Toast.LENGTH_SHORT).show();
		}
		
	}

	@Override
	public void onDestroy() {
		if(adapter!=null){
			adapter.imageLoader.stopThread();
		}
		if(adapterP!=null){
			adapterP.imageLoader.stopThread();
		}
		list.setAdapter(null);
		super.onDestroy();
	}
	
}
